/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

/*********************
 *      INCLUDES
 *********************/
#define _DEFAULT_SOURCE /* needed for usleep() */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <aos/aos.h>
#include "app_config.h"
#include "app_init.h"
#include "csi_config.h"
#include "hw_config.h"

#include "board_config.h"
#include <drv/pin.h>
#include <drv/pwm.h>

#ifdef CONFIG_PWM_MODE

#define CHANEL_R 3 //(7/2)
#define CHANEL_G 1 //(2/2)
#define CHANEL_B 2 //(4/2)

#define PULSE_MAX 300
#define PULSE_MIN 0

static uint32_t g_ctr = 0;
static int32_t g_pulse_width = PULSE_MIN;
static bool g_pulse_dir = true;

static csi_pwm_t  r;
void led_pinmux_init()
{
    csi_error_t ret;
    csi_pin_set_mux(PA7, PA7_PWM_CH7);
    csi_pin_set_mux(PA25, PA25_PWM_CH2);
    csi_pin_set_mux(PA4, PA4_PWM_CH4);
	
    ret = csi_pwm_init(&r, 0);
    if (ret != CSI_OK) 
    {
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
	
	//7
    ret = csi_pwm_out_config(&r, CHANEL_R, 300, 100, PWM_POLARITY_HIGH);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
    ret = csi_pwm_out_start(&r, CHANEL_R);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
        
    //2
    ret = csi_pwm_out_config(&r, CHANEL_G, 300, 100, PWM_POLARITY_HIGH);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
    ret = csi_pwm_out_start(&r, CHANEL_G);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
	 
	//4
    ret = csi_pwm_out_config(&r, CHANEL_B, 300, 100, PWM_POLARITY_HIGH);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
    ret = csi_pwm_out_start(&r, CHANEL_B);
    if (ret != CSI_OK) 
	{
		printf("===%s, %d\n", __FUNCTION__, __LINE__);
		return ;
    }
	
	csi_pwm_out_stop(&r, CHANEL_R);
	csi_pwm_out_stop(&r, CHANEL_G);
	csi_pwm_out_stop(&r, CHANEL_B);
}

void led_refresh()
{
    if (g_ctr == 0)
    {
		csi_pwm_out_config(&r, CHANEL_R, PULSE_MAX, g_pulse_width, PWM_POLARITY_LOW);
        csi_pwm_out_start(&r, CHANEL_R);
        csi_pwm_out_stop(&r, CHANEL_G);
        csi_pwm_out_stop(&r, CHANEL_B);

    }
    else if (g_ctr == 1)
    { 
		csi_pwm_out_config(&r, CHANEL_G, PULSE_MAX, g_pulse_width, PWM_POLARITY_LOW);       
        csi_pwm_out_start(&r, CHANEL_G);
        csi_pwm_out_stop(&r, CHANEL_R);
        csi_pwm_out_stop(&r, CHANEL_B);

    }
    else //2
    {
		csi_pwm_out_config(&r, CHANEL_B, PULSE_MAX, g_pulse_width, PWM_POLARITY_LOW);
        csi_pwm_out_start(&r, CHANEL_B);
        csi_pwm_out_stop(&r, CHANEL_R);
        csi_pwm_out_stop(&r, CHANEL_G);

    }
	
	if (g_pulse_dir)
	{
		g_pulse_width += 2;
		if (g_pulse_width > PULSE_MAX)
		{
			g_pulse_width = PULSE_MAX;
			g_pulse_dir = false;
		}
	}
	else
	{
		g_pulse_width -= 2;
		if (g_pulse_width < PULSE_MIN)
		{
			g_pulse_width = PULSE_MIN;
			g_pulse_dir = true;
			
			g_ctr++;
			if (g_ctr == 3) 
			{
				g_ctr = 0;
			}
		}
	}
}
void marquee_test(void)
{
    while (1);
}
#endif 
